package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetMealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    DishFlavorService dishFlavorService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    SetMealService setMealService;
    @Override
    @Transactional
    public void saveWithFlavors(DishDto dishDto) {
        List<DishFlavor> flavors = dishDto.getFlavors();

        //保存口味
        this.save(dishDto);

        //给每一个口味，添加菜品id
        flavors.stream().forEach(item -> item.setDishId(dishDto.getId()));

        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public List<DishDto> listWithFlavors(Long categoryId, Integer status) {
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("category_id", categoryId)
                .eq("status", status);
        List<Dish> list = this.list(queryWrapper);
        List<DishDto> dishDtoList = new ArrayList<>();
        list.forEach(dish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            List<DishFlavor> flavors = dishFlavorService.list(new QueryWrapper<DishFlavor>().eq("dish_id", dish.getId()));
            dishDto.setFlavors(flavors);
            dishDtoList.add(dishDto);
        });
        return dishDtoList;
    }

    @Override
    public Page<DishDto> selectPageByName(Integer page, Integer pageSize, String name) {

        Page<Dish> dishPage = new Page<>(page, pageSize);
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<Dish>().like(!StringUtils.isEmpty(name), "name", name);
        this.page(dishPage, queryWrapper);


        List<DishDto> collect = dishPage.getRecords().stream().map(item -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            dishDto.setCategoryName(categoryService.getById(item.getCategoryId()).getName());
            return dishDto;
        }).collect(Collectors.toList());


        Page<DishDto> dishDtoPage = new Page<>();
        dishDtoPage.setTotal(dishPage.getTotal());
        dishDtoPage.setRecords(collect);
        return dishDtoPage;
    }

    @Override
    public DishDto selectById(Long id) {
        Dish byId = this.getById(id);
        QueryWrapper<DishFlavor> queryWrapper = new QueryWrapper<DishFlavor>().eq("dish_id", id);
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(byId, dishDto);
        dishDto.setFlavors(flavors);
        Category category = categoryService.getById(byId.getCategoryId());
        dishDto.setCategoryName(category.getName());
        return dishDto;
    }

    @Override
    public void updateStatusByIds(Integer status, Long[] ids) {
        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            this.updateById(dish);
        }
    }

    @Override
    public void deleteByIds(Long[] ids) {
        for (Long id : ids) {
            List<DishFlavor> list = dishFlavorService.list(new QueryWrapper<DishFlavor>().eq("dish_id", id));
            if (list.size() > 0) {
                throw new CustomException("当前关联下的口味未删除！");
            }
            List<Setmeal> setMeals = setMealService.list(new QueryWrapper<Setmeal>().eq("dish_id",id));
            if(setMeals.size()>0){
                throw new CustomException("当前关联下的套餐未删除！");
            }
            this.removeById(id);
        }
    }
}
